package driver

import (
	"fmt"
	"gitee.com/xluomo945/fm/setting"
	"github.com/gin-gonic/gin"
	syslog "log"
)

type consoleWriter struct {
	isShowTrace bool
}

func (w *consoleWriter) OpenTrace() {
	w.isShowTrace = true
}

func ConsoleCreate(_ setting.LoggerConfig) (setting.LogWriter, error) {
	return &consoleWriter{}, nil
}

func (w *consoleWriter) write(ctx *gin.Context, flag string, msg string) {
	if w.isShowTrace {
		syslog.Println(fmt.Sprintf("[%s][%s] %s", trace(ctx), flag, msg))
	} else {
		syslog.Println(fmt.Sprintf("[%s] %s", flag, msg))
	}
}

func (w *consoleWriter) Info(ctx *gin.Context, msg string) {
	w.write(ctx, "INFO", msg)
}

func (w *consoleWriter) Warn(ctx *gin.Context, msg string) {
	w.write(ctx, "WARN", msg)
}

func (w *consoleWriter) Fail(ctx *gin.Context, msg string) {
	w.write(ctx, "FAIL", msg)
}

func (w *consoleWriter) Custom(ctx *gin.Context, flag string, msg string) {
	w.write(ctx, flag, msg)
}

func (w *consoleWriter) Close() {}
